<?php
class NavigationController extends BaseManageController{
	public function actionAdmin(){
		$this->pageTitle = '导航组管理 - ' . Yii::app()->name;
		$mod = new NavigationMondel();
		$navs = $mod->find();
		$now = time();
		if(!empty($navs)){
			//统计有效导航数
			foreach($navs as &$_nav){
				$available_count = 0;
				if(isset($_nav['items'])){
					$_nav['total'] = count($_nav['items']); //总数
				}else{
					$_nav['total'] = 0;
				}
			}
		}
		$this->render('admin', array(
			'navs' => $navs
		));
	}
	
	public function actionEdit($id = null){
		if(Yii::app()->request->isPostRequest){
			$this->_save($id);
		}
		$this->pageTitle = '导航组编辑 - ' . Yii::app()->name;
		$mod = new NavigationMondel();
		$nav = null;
		if($id !== null){
			$nav = $mod->get_by_key($id);
		}
		if(empty($nav)){
			$nav = array(
				'_id' 	=> '',
				'name' 	=> '导航组名称'
			);
		}else{
			if(isset($nav['items'])){
				foreach ($nav['items'] as $index=>&$item){
					if(isset($item['start'])){
						$item['start'] = date('Y-m-d', $item['start']);
					}
					if(isset($item['end'])){
						$item['end'] = date('Y-m-d', $item['end']);
					}
				}
				$tree = new YTree($nav['items'], array(
					'id_column' 	=> 'id',
					'name_column'	=> 'title'
				));
				$this->assign('items', $tree->get_tree());
			}
		}
		
		$this->render('edit', array(
			'model' => $nav
		));
	}
	
	private function _save($id){
		$nav = $_POST['Nav'];

		$mod = new NavigationMondel();
		//检查是否有重名的
		$condition = array();
		if(empty($nav['_id'])){
			//新增
			$condition['code'] = $nav['code'];
		}else{
			$condition = array(
				'code' => $nav['code'],
				'_id' => array('$ne'=>$nav['_id'])
			);
		}
		if($mod->exists_attrs($condition)){
			$this->ajax_error('导航组代码：“' . $nav['code'] . '”已经存在！');
		}
		
		if(isset($_POST['titles'])){
			$titles 	= $_POST['titles'];
			$urls 		= $_POST['urls'];
			$starts 	= $_POST['starts'];
			$ends 		= $_POST['ends'];
			$descs 		= $_POST['descs'];
			$targets 	= $_POST['targets'];
			$pictures 	= $_POST['pictures'];
			$parents 	= $_POST['parents'];
			$temp_ids 	= $_POST['temp_ids'];
			
			$path = IMAGE_PATH . 'navigation/' . date('Y/m/d/');
			CommonHelper::ymkdirs(ROOT_PATH . $path);
			
			$nav['items'] = array();
			//处理图片
			foreach ($titles as $index=>$title){
				$p = $pictures[$index];
				$item = array(
					'id'		=> $temp_ids[$index],
					'title' 	=> $title,
					'url'		=> $urls[$index],
					'desc'		=> $descs[$index],
					'target' 	=> $targets[$index],
					'parent'	=> $parents[$index],
				);
				if(preg_match('/.*\/uploads\/temp\/(.*)/', $p, $matchs)){
					$img_name = $matchs[1];
					rename(ROOT_PATH . $p, ROOT_PATH . $path . $img_name);
					$p = $path . $img_name;
					$item['picture'] = $p;
				}else{
					if(!empty($p)){
						$item['picture'] = $p;
					}
				}
				$start_str = $starts[$index];
				$end_str = $ends[$index];
				if(preg_match('/^\d{4}\-\d{1,2}\-\d{1,2}$/', $start_str)){
					//如果是日期格式
					$item['start'] = strtotime($start_str);
				}
				if(preg_match('/^\d{4}\-\d{1,2}\-\d{1,2}$/', $end_str)){
					//如果是日期格式
					$item['end'] = strtotime($end_str) + 3600 * 24 - 1;
				}
				$nav['items'][] = $item;
			}
		}
		$id = null;
		if(empty($nav['_id'])){
			$id = CommonHelper::gen_uuid();
			$nav['_id'] = $id;
			$nav['site_id'] = SITE_ID;
			$mod->insert($nav);
			$id = $id->__toString();
		}else{
			$id = $nav['_id'];
			$mod->update_by_key($id, $nav);
		}
		$this->ajax_info('保存成功！', $id);
	}
	
	public function actionDel($id){
		$mod = new NavigationMondel();
		$mongo_id = new $id;
		$nav = $mod->get_by_key($mongo_id);
		if(empty($nav)){
			$this->ajax_error('您要删除的记录不存在或已被删除！');
		}
		if($nav['site_id'] != SITE_ID){
			$this->ajax_error('您无删除此记录的权限！');
		}
		$mod->delete_by_key($mongo_id);
		$this->ajax_info('删除成功！');
	}
}
?>